package com.gmail.gwanndan.common.auth;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import com.gmail.gwanndan.common.hibernate.AbstractEntity;

/**
 * @author Dennis Ahaus, dennis.ahaus@gmail.com
 */
@Entity
@Table(name = "users")
public class User extends AbstractEntity {

	@Id
	@GeneratedValue
	private int id = -1;

	private String username;
	private String password;

	@ManyToMany
	@Cascade(value = { CascadeType.ALL })
	@JoinTable(name = "users_has_permissions")
	private List<Permission> permissions;

	@ManyToMany
	@Cascade(value = { CascadeType.ALL })
	@JoinTable(name = "users_has_roles")
	private List<Role> roles = new LinkedList<Role>();

	public User() {

	}

	public User(String username, String password) {
		this(username, password, new LinkedList<Role>());
	}

	public User(String username, String password, List<Role> roles) {
		super();
		this.username = username;
		this.password = password;
		this.roles = roles;
	}

	public String toString() {
		return "" + getId() + " - " + getUsername();
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * Get list of {@link Role}s this user has
	 * 
	 * @see User#roles
	 */
	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public List<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

}
